package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.constant.MessageConstant;
import com.atguigu.dao.MemberDao;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.pojo.Member;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderService;
import com.atguigu.utils.DateUtils;
import com.atguigu.vo.Result;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * @program: meinian_parent
 * @author: Mr.Zhuo
 * @create: 2022-02-17 20:55
 **/
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao orderDao;

    @Autowired
    OrderSettingDao orderSettingDao;

    @Autowired
    MemberDao memberDao;

    @Override
    public Map findById(Integer id) {
        return orderDao.findById(id);
    }





    /**
     * // 我们来分析具体的的业务逻辑
     * 1.首先判断当前，是否后台可以预约（1.可能没有开放2.可预约人数为0 ）
     * 2.如果可以预约，判断当前是否预约已满
     * 3.然后判断当前用户是否是已注册用户，如果是则可以下单预约,如果不是则直接注册账户
     * 4.判断当前用户对于此套餐是否存在重复预约（如果重复预约则不能预约）
     * 5.下单预约（保存订单，预约人数加1）
     */

    @Override
    public Result save(Map map) throws Exception {
        // 首先处理一下传输过来的map数据
        String orderDateString = (String) map.get("orderDate");
        String phoneNumber = (String) map.get("telephone");
        String idCard = (String) map.get("idCard");
        String sex = (String) map.get("sex");
        String name = (String) map.get("name");
        Integer setmealId = Integer.parseInt((String) map.get("setmealId"));
        Date orderDate = DateUtils.parseString2Date(orderDateString);
        //1.首先判断当前，是否后台可以预约（1.可能没有开放2.可预约人数为0 ）
        long count = orderSettingDao.findCountByOrderDate(orderDate);
        if (count <= 0) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.如果可以预约，判断当前是否预约已满
        OrderSetting orderSetting = orderSettingDao.getOrderSettingSignleByMonth(orderDate);
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (!(number > reservations)) {
            // 已经预约已满
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //3.然后判断当前用户是否是已注册用户，如果是则可以下单预约,如果不是则直接注册账户
        // 每个手机号表示唯一性
        Member member = memberDao.getByTelephone(phoneNumber);

        if (member == null) {
            // 客户不存在，需要直接注册
            member = new Member();
            member.setName(name);
            member.setSex(sex);
            member.setIdCard(idCard);
            member.setPhoneNumber(phoneNumber);
            member.setRegTime(orderDate);
            //主键自增
            // 给用户直接注册
            memberDao.registerMember(member);
        } else {
            // 客户存在
            //4.判断当前用户对于此套餐是否存在重复预约（如果重复预约则不能预约）\
            Integer memberId = member.getId();
            Order order = new Order(memberId, orderDate, null, null, setmealId);
            Order orderDB = orderDao.getOrder(order);
            if (orderDB != null) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }


        //5.下单预约（保存订单，预约人数加1）
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);


        Order order = new Order();
        order.setMemberId(member.getId()); //会员id
        order.setOrderDate(orderDate); // 预约时间
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        orderDao.save(order);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order);
    }
}
